var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
  return typeof o;
} : function (o) {
  return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
}, app = getApp();
var app = getApp();
var util = require("../../utils/util.js");
var base = require('./../../../config.js');
var WxParse = require('./../wxParse/wxParse.js');
Page({
  data: {
    time: "17:00",
    num: 1,
    see_price: !1,
    red_bag: 0,
    red_bag_id: 0,
    mode1: "success",
    mode2: "clear",
    mode3: "clear",
    pay_mode: !1,
    refrer_to: "确认支付",
    type: 1,
    pay_num: 1,
    url: wx.getStorageSync("url"),
    user_name: '',
    yj_cost: 0,
    settlement: '0.00',
    hasI: false,
    noI: '',
    interests_list: [],
    pay_num: 1,
    url: wx.getStorageSync("url"),
    user_name: '',
    yj_cost: 0,
    settlement: '0.00',
    hasI: false,
    noI: '',
    interests_list: [],
    inter_id: '',
    selectInter: false,
    selectInfo: '',
    selectInF: false,
    originPrice: 0,
    noList: 0
  },
  radioChange: function (e) {
    this.setData({
      inter_id: e.detail.value ? e.detail.value*1 : ''
    })
    console.log(e.detail.value);
    console.log(this.data.originPrice);
    if (e.detail.value != '') {
      this.setData({
        selectInfo: this.data.interests_list[e.detail.value],
        selectInF: true,
        settlement: (this.data.originPrice * 1 * (this.data.interests_list[e.detail.value].rebate * 1)*0.1).toFixed(2)
      })
    } else {      
      this.setData({
        selectInfo: [],
        selectInF: false,
        settlement: this.data.originPrice
      })
    }
  },
  payInterests () {
    // 开通权益卡
    var that = this;
    wx.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=AddcardOrder&m=zh_jdgjb&user_id=" + wx.getStorageSync("userInfo").id + '&equitycard_id=' + that.data.noI.id + '&price=' + that.data.noI.cost + '&out_trade_no=' + Date.parse(new Date()),
      // url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=AddcardOrder&m=zh_jdgjb&user_id=1" + '&equitycard_id=' + that.data.noI.id + '&price=' + that.data.noI.cost,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
        app.util.request({
          url: "entry/wxapp/Pay",
          cachetime: "0",
          data: {
            openid: app.OpenId,
            money: that.data.noI.cost,
            order_id: res.data.data.order_id,
            type: 'card'
          },
          success: function (o) {
            console.log(o);
            wx.requestPayment({
              timeStamp: o.data.timeStamp,
              nonceStr: o.data.nonceStr,
              package: o.data.package,
              signType: o.data.signType,
              paySign: o.data.paySign,
              success: function (o) {
                console.log(o);
                wx.showToast({
                  title: '支付成功'
                })
                that.setData({
                  selectInter: !that.data.selectInter
                })
                // 重新请求旅行卡数据
                wx.request({
                  url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=MyCard&m=zh_jdgjb&user_id=" + wx.getStorageSync("userInfo").id + '&card_type=1',
                  header: {
                    'content-type': 'application/json' // 默认值
                  },
                  success: function (res) {
                    console.log(res);
                    // 如果有权益卡
                    if (res.data.data.open != '') {
                      that.setData({
                        hasI: true,
                        interests_list: res.data.data.open,
                      })
                    } else {
                      that.setData({
                        noI: res.data.data.notopen[0]
                      })
                      WxParse.wxParse('privilege', 'html', that.data.noI.privilege, that, 5);
                      WxParse.wxParse('introduce', 'html', that.data.noI.introduce, that, 5);
                    }
                  }
                });
              },fail:function () {
                wx.showToast({
                  title: '支付失败',
                  icon: 'none'
                })
                that.setData({
                  selectInter: !that.data.selectInter
                })
              }
            })
          }
        })
      }
    });
  },
  // 点击了确定按钮
  makeSure: function (e) {
    console.log(e.currentTarget.dataset.sign);
    var sign = 0;
    if (e.currentTarget.dataset.sign == 'noList') {
      sign = 0;
    } else {
      sign = 1;
    }
    this.setData({
      selectInter: !this.data.selectInter,
      noList: sign
    })
  },
  onLoad: function (o) {
    var t = this;
    // 查询优惠卡
    wx.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=MyCard&m=zh_jdgjb&user_id=" + wx.getStorageSync("userInfo").id + '&card_type=1',
      // url: base.siteroot + "?i=" + base.acid + '&c=entry&a=wxapp&do=MyCard&m=zh_jdgjb&user_id=1&card_type=1',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
        // 如果有权益卡
        if (res.data.data.open != '') {
          t.setData({
            hasI: true,
            interests_list: res.data.data.open,
          })
        } else {
          t.setData({
            noI: res.data.data.notopen[0]
          })
          WxParse.wxParse('privilege', 'html', t.data.noI.privilege, t, 5);
          WxParse.wxParse('introduce', 'html', t.data.noI.introduce, t, 5);
        }
      }
    });
    t.setData({
      color: wx.getStorageSync("platform").color
    }), wx.setNavigationBarColor({
      frontColor: '#000000',
          backgroundColor: '#ffffff'
    }), console.log(o);
    var e = o.room_id, a = o.hotel_id, n = wx.getStorageSync("day1"), s = wx.getStorageSync("day2"), c = wx.getStorageSync("day");
    t.setData({
      room_id: e,
      hotel_id: a,
      day1: n,
      day2: s,
      day: c,
      coupon: 0,
      condition: 0,
      coupons_id: -1,
      form_d: o.form_d,
      grade: wx.getStorageSync("platform").open_member,
      system: wx.getStorageSync("platform")
    });
    var r = 0;
    app.util.request({
      url: "entry/wxapp/GetRoomCost",
      data: {
        room_id: e,
        start: n,
        end: s
      },
      success: function (o) {
        for (var e in console.log(o), o.data) r += Number(o.data[e].mprice);
        console.log(r), t.setData({
          z_price: r,
          price_infos: o.data
        }), t.refresh(), t.room_num();
      }
    });
  },
  agreement () {
    let that = this;
    wx.setStorageSync('agreement', that.data.noI.agreement)
    wx.navigateTo({
      url: './../interests/agreement?'
    })
  },
  userinfo: function (o) {
    var e = this;
    app.getUserInfo(function (o) {
      console.log(o), e.setData({
        userInfo: o
      });
    });
  },
  refresh: function (o) {
    var a = this, e = a.data.room_id, t = a.data.hotel_id;
    2 == a.data.grade ? (a.setData({
      discount: 1
    }), a.data.z_price) : app.getUserInfo(function (o) {
      var e = o.id;
      console.log("会员等级为 " + e);
      var t = o.discount;
      null == t && (t = 10), console.log("会员折扣为 " + t), a.setData({
        discount: t / 10
      }), null != a.data.z_price && a.cost();
    });
    var n = wx.getStorageSync("userInfo").id;
    app.util.request({
      url: "entry/wxapp/MyCoupons",
      cachetime: "0",
      data: {
        user_id: n
      },
      success: function (o) {
        console.log("我的优惠券"), console.log(o), a.setData({
          coupons: o.data
        });
      }
    }), app.util.request({
      url: "entry/wxapp/RoomDetails",
      cachetime: "0",
      data: {
        room_id: e
      },
      success: function (o) {
        console.log(o);
        o.data.price, o.data.yj_cost;
        a.setData({
          room: o.data,
          yj_cost: Number(o.data.yj_cost)
        }), a.cost();
      }
    }), app.util.request({
      url: "entry/wxapp/PjDetails",
      cachetime: "0",
      data: {
        seller_id: t
      },
      success: function (o) {
        console.log(o), a.setData({
          hotel: o.data
        });
      }
    }), app.util.request({
      url: "entry/wxapp/MyHb",
      cachetime: "0",
      data: {
        user_id: n,
        page: 1
      },
      success: function (o) {
        console.log(o), a.setData({
          my_hb: o.data
        });
      }
    });
  },
  room_num: function (o) {
    var n = this, e = wx.getStorageSync("day1"), t = wx.getStorageSync("day2"), a = n.data.room_id;
    app.util.request({
      url: "entry/wxapp/GetRoomNum",
      data: {
        room_id: a,
        start: e,
        end: t
      },
      success: function (o) {
        console.log(o);
        var e = [], t = [];
        for (var a in o.data.map(function (o) {
          var e;
          e = Number(o.nums), t.push(e);
        }), o.data) o.data[a].nums <= 0 && e.push(o.data[a]);
        t = t.sort(app.sort_num_order), console.log(t), n.setData({
          rooms: t[0]
        }), 0 < e.length && wx.showModal({
          title: "",
          content: e[0].dateday + "没有房间了",
          success: function (o) {
            o.confirm ? (console.log("用户点击确定"), wx.navigateBack({
              delta: 1
            })) : o.cancel && (console.log("用户点击取消"), wx.navigateBack({
              delta: 1
            }));
          }
        });
      }
    });
  },
  use_res_bag: function (o) {
    wx.navigateTo({
      url: "../coupon/red_bag"
    });
  },
  bindTimeChange: function (o) {
    console.log(o), console.log("picker发送选择改变，携带值为", o.detail.value), this.setData({
      time: o.detail.value
    });
  },
  add_num: function (o) {
    var e = this;
    if (e.data.rooms == e.data.num) wx.showModal({
      title: "",
      content: "没有这么多房间啦"
    }); else {
      var t = e.data.num + 1;
      e.setData({
        num: t
      }), e.cost();
    }
  },
  reduce_num: function (o) {
    var e = this.data.num - 1;
    1 <= e && (this.setData({
      num: e
    }), this.cost());
  },
  see_price: function (o) {
    console.log(o);
    var e = this.data.see_price;
    1 == e ? this.setData({
      see_price: !1
    }) : this.setData({
      see_price: !0
    });
  },
  mode1: function (o) {
    this.setData({
      mode1: "success",
      mode2: "clear",
      mode3: "clear",
      refrer_to: "确认支付",
      type: 1
    }), this.refresh();
  },
  mode2: function (o) {
    this.setData({
      mode1: "clear",
      mode2: "success",
      mode3: "clear",
      refrer_to: "确认支付",
      yj_cost: 0,
      type: 3
    }), this.cost();
  },
  mode3: function (o) {
    var e = this, t = e.data.hotel, a = e.data.userInfo;
    console.log(a), 2 == t.ye_open ? e.setData({
      refrer_to: "该酒店不支持余额支付"
    }) : Number(a.balance) < e.data.settlement ? wx.showModal({
      title: "",
      content: "余额不足,请先去充值"
    }) : (e.setData({
      refrer_to: "确认支付",
      type: 2
    }), e.setData({
      mode1: "clear",
      mode2: "clear",
      yj_cost: 0,
      mode3: "success"
    }), e.cost());
  },
  cost: function (o) {
    var e = this, t = e.data.num, a = Number(e.data.yj_cost);
    console.log(a);
    var n = a * t;
    console.log(n);
    var s = e.data.z_price;
    console.log(s);
    var c = e.data.coupon, r = Number(e.data.red_bag), d = Number(s) * t, i = d * e.data.discount, l = d - i, u = i - c + n - r, p = d - c - r;
    (p = p.toFixed(2)) <= 0 && (p = 0), i = i.toFixed(2), l = l.toFixed(2);
    var g = d;
    (u = u.toFixed(2)) <= 0 && (u = 0 != a ? n : .01), d = (d += n).toFixed(2), g = g.toFixed(2),
      console.log("房间总金额为 " + g), console.log("折扣价为 " + i), console.log("折扣的金额为 " + l),
      console.log("最后结算金额为 " + u),console.log('金额为' + e.data.u), e.setData({
        price: d,
        total_price: g,
        settlement: isNaN(u)?'0.00':u,
        discount_price: i,
        reduction_price: l,
        dis_cost: p,
        yj_price: n,
        originPrice: g
      });
  },
  use_coupon: function (o) {
    var e = this.data.discount_price, t = this.data.hotel_id;
    wx.navigateTo({
      url: "../coupon/store_coupon?money=" + e + "&hotel_id=" + t
    });
  },
  bindinputText: function (e) {
    console.log(e.detail.value);
    this.setData({
      user_name: e.detail.value
    })
  },
  pay_mode: function (o) {
    if (this.data.user_name == '') {
      wx.showModal({
        title: '温馨提醒',
        content: '入住人信息不能为空'
      })
      return false;
    }
    var e = this.data.pay_mode;
    1 == e ? this.setData({
      pay_mode: !1
    }) : this.setData({
      pay_mode: !0
    });
  },
  DateDiff: function(sDate1, sDate2) {    //sDate1和sDate2是2006-12-18格式  
    var aDate, oDate1, oDate2, iDays
    aDate = sDate1.split("-")
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])    //转换为12-18-2006格式  
    aDate = sDate2.split("-")
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)    //把相差的毫秒数转换为天数  
    return iDays
  },
  formSubmit: function (o) {
    var e = this, t = e.data.form_d;
    console.log(t);

    // wx.showModal({
    //   title: '查找formID',
    //   content: o.detail.formId,
    // })

    var a = e.data.userInfo, n = o.detail.formId, s = e.data.hotel, c = e.data.room, r = o.detail.value.code, d = e.data.total_price, i = e.data.settlement, l = e.data.condition, u = e.data.price, p = e.data.user_name, g = o.detail.value.tel, m = e.data.time, _ = s.id, f = c.id, y = wx.getStorageSync("userInfo").id, h = e.data.coupons_id, x = s.name, w = s.address, v = s.coordinates, S = e.data.day1, b = e.data.day2, D = u, I = e.data.num, T = c.name, q = c.size, M = e.DateDiff(b, S), j = e.data.settlement, z = e.data.yj_cost, N = e.data.coupon, k = c.logo, C = e.data.reduction_price, F = e.data.red_bag, P = e.data.red_bag_id, R = e.data.type;
    console.log("入住人为 " + p), console.log("联系人电话为 " + g), console.log("房间价格为 " + D),
      console.log("选择的到店时间为 " + m), console.log("酒店id " + _), console.log("房间id " + f),
      console.log("用户id " + y), console.log("优惠券id " + h), console.log("酒店名字 " + x), console.log("酒店地址 " + w),
      console.log("酒店经纬度 " + v), console.log("到店日期 " + S), console.log("离店日期 " + b), console.log("结算总金额 " + D),
      console.log("房间数量 " + I), console.log("房间类型 " + T), console.log("床型 " + q), console.log("入住天数 " + M),
      console.log("折扣后的价格 " + j), console.log("押金金额 " + z), console.log("优惠券金额 " + N),
      console.log("支付方式为 " + R);
    var B = e.data.system, L = "";
    if ("" == p) L = "请填写入住人姓名"; else if ("" == g) L = "请填写联系电话"; else if ("" == r && 1 == B.is_sfz) L = "请填写您的身份证号"; else if (2 == B.is_sfz) r = ""; else 0 == /^1[3|4|5|7|8|9][0-9]\d{4,8}$/.test(g) ? L = "请输入正确的手机号" : 0 < N ? l > e.data.discount_price && (L = "不满足优惠券满减条件") : i <= 0 && (i = .01);

    var O = e.data.pay_num;
    "" != L ? (wx.showModal({
      title: "温馨提示",
      content: L
    }), e.setData({
      pay_num: 1
    })) : "" == L && 1 == O && (e.setData({
      pay_num: 0
    }), "" == a.tel ? wx.showModal({
      content: "请先绑定手机号",
      success: function (o) {
        o.confirm && (console.log("用户点击确定"), wx.navigateTo({
          url: "../register/register"
        }));
      }
    }) : app.util.request({
      url: "entry/wxapp/AddOrder",
      data: {
        name: p,
        tel: g,
        price: d,
        dd_time: m,
        seller_id: _,
        room_id: f,
        user_id: y,
        coupons_id: h,
        seller_name: x,
        seller_address: w,
        coordinates: v,
        arrival_time: S,
        departure_time: b,
        num: I,
        room_type: T,
        bed_type: q,
        days: M,
        dis_cost: j,
        yj_cost: z,
        yhq_cost: N,
        total_cost: d,
        room_logo: k,
        yyzk_cost: C,
        hb_id: P,
        hb_cost: F,
        from_id: t,
        classify: 1,
        type: R,
        code: r,
        equitycard_id: e.data.selectInfo.id ? e.data.selectInfo.id : '',
        equitycard_cost: e.data.selectInfo.rebate ? e.data.selectInfo.rebate : ''
      },
      success: function (o) {
        console.log("这是提交订单"), console.log(o);
        var t = o.data, a = o.data;
        if (1 == R) {
          t = void 0 === t ? "undefined" : _typeof(t);
          var e = app.OpenId;
          "string" != t ? app.util.request({
            url: "entry/wxapp/Pay",
            cachetime: "0",
            data: {
              openid: e,
              money: i,
              order_id: a
            },
            success: function (o) {
              wx.requestPayment({
                timeStamp: o.data.timeStamp,
                nonceStr: o.data.nonceStr,
                package: o.data.package,
                signType: o.data.signType,
                paySign: o.data.paySign,
                success: function (o) {
                  console.log("支付成功"), console.log(o), 
                    wx.navigateTo({
                      url: './../order/book?orderid=' + a,
                    });
                  // wx.redirectTo({
                  //   url: "../order/order?activeIndex=0&index=0"
                  // });
                  var e = wx.getStorageSync("userInfo").openid;
                  console.log(n), console.log(e), console.log(t), wx.hideLoading(), app.util.request({
                    url: "entry/wxapp/Message",
                    cachetime: "0",
                    data: {
                      form_id: n,
                      openid: e,
                      order_id: a
                    },
                    success: function (o) {
                      console.log("发送模板消息"), console.log(o);
                    }
                  });
                },
                fail: function (o) {
                  console.log("支付失败"), wx.showToast({
                    title: "支付失败"
                  }), wx.redirectTo({
                    url: "../order/order?activeIndex=1&index=1"
                  });
                }
              });
            }
          }) : wx.showModal({
            content: o.data
          });
        } else if (2 == R) {
          t = void 0 === t ? "undefined" : _typeof(t);
          e = app.OpenId;
          "string" != t ? app.util.request({
            url: "entry/wxapp/YePay",
            cachetime: "0",
            data: {
              order_id: a
            },
            success: function (o) {
              wx.hideLoading(), wx.showToast({
                title: "支付成功"
              }), setTimeout(function () {
                wx.redirectTo({
                  url: "../order/order?activeIndex=0&index=0"
                });
              }, 1500);
              var e = wx.getStorageSync("userInfo").openid;
              app.util.request({
                url: "entry/wxapp/Message",
                cachetime: "0",
                data: {
                  form_id: n,
                  openid: e,
                  order_id: a
                },
                success: function (o) {
                  console.log("发送模板消息"), console.log(o);
                }
              });
            }
          }) : wx.showModal({
            content: o.data
          });
        } else 3 == R && (wx.hideLoading(), wx.showToast({
          title: "订单提交成功"
        }), setTimeout(function () {
          wx.redirectTo({
            url: "../order/order?activeIndex=0&index=0"
          });
        }, 1500));
      }
    }));
  },
  onReady: function () { },
  onShow: function () {
    this.userinfo(), -1 != this.data.coupons_id ? this.cost() : "" == this.data.coupons_id && this.cost(),
      0 != this.data.red_bag && this.cost();
  },
  onHide: function () { },
  onUnload: function () { },
  onPullDownRefresh: function () { },
  onReachBottom: function () { }
});